
package my.WebChat;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Marta
 */
public class ChatCliente extends javax.swing.JFrame {

    /**
     * Creates new form ChatCliente
     */
    Socket clientSocket;
    PrintWriter escritor;
    JTextField textoParaEnviar;
    
    public ChatCliente() {
//        initComponents();
        super("Chat");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,90);
        setVisible(true);
        textoParaEnviar = new JTextField();
        JButton botao = new JButton("Enviar");
        botao.addActionListener(new EnviarListener());
        Container envio = new JPanel();
        envio.setLayout(new BorderLayout());
        envio.add(BorderLayout.CENTER, textoParaEnviar);
        envio.add(BorderLayout.EAST, botao);
        getContentPane().add(BorderLayout.SOUTH, envio);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 120);

    }

    /* This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chat");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 458, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 116, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    private class EnviarListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            configurarRede();
            escritor.println(criptografa(textoParaEnviar.getText()));
            escritor.flush();
            textoParaEnviar.setText("");
            textoParaEnviar.requestFocus();
        }

    }

    private void configurarRede() {
        try {
            clientSocket = new Socket("192.168.0.101", 7000);
            escritor = new PrintWriter(clientSocket.getOutputStream());
        } catch (Exception e) {
            System.out.println(e.toString());
            
        }

    }
    
    private String criptografa(String mensagem)
    {
        int i, letraNum;
        char [] letras = mensagem.toCharArray();
        char letra;
        String mensCript = "";
       
        for (i=0;i<mensagem.length();i++)
        {
            letraNum = (int) letras[i];
            letraNum = letraNum + i + mensagem.length() +  555;
            letra = (char) letraNum;
            mensCript  = mensCript + letra;
        }
        return mensCript;
    }

    public static void main(String args[]) {
        new ChatCliente();
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(ChatCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(ChatCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(ChatCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(ChatCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new ChatCliente().setVisible(true);
//            }
//        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
